<?php
/**
 * Created by PhpStorm.
 * User: dell
 * Date: 2018/5/29
 * Time: 20:00
 */

namespace app\model;


class UserMaterial extends Model
{
    public function __construct($db)
    {
        parent::__construct($db,"user_material");
    }


    /**
     * 视频详情公用字段组件
     */
    protected function videoDetailFieldComponent()
    {
        $db = $this->db;
        $db->select("a.user_material_uuid,a.ori_material_uuid,a.user_uuid,a.video_name,music_video_url as video_url,a.pic,a.nickname,a.head_image,if(a.custom_zan>0,a.custom_zan,zan) as zan,a.not_zan,a.time_length,UNIX_TIMESTAMP(a.add_time) as add_time,a.pv_num,a.vv_num,a.share_num,b.dub_number");
        $db->from("user_material as a");
        $db->join("video as b","a.ori_material_uuid=b.material_id");
    }

    /**
     * 列表页数据
     * @param array $where
     * @param array $order
     * @param $page
     * @param $pageSize
     * @return mixed
     */
    public function getInterfaceMaterialList(array $where,array $order,$page,$pageSize)
    {
        $db = $this->db;
        $this->videoDetailFieldComponent();
        $db->where(["a.mix_status"=>1,"a.is_save"=>1]);
        $db->where($where);
        $total = $db->count_all_results();
        $db->order_by($order);
        $db->limit($pageSize,($page-1)*$pageSize);
        $query = $db->get();
        $materialList = $query->all();
        $data["total"] = $total;
        $data["data"] = $materialList;
        return $data;
    }

    /**
     * 素材详情
     * @param $userMaterialUUID
     * @return array|mixed
     */
    public function getMaterialDetail($userMaterialUUID)
    {
        $db = $this->db;
        $this->videoDetailFieldComponent();
        $db->where(["a.status"=>2,"a.mix_status"=>1,"a.is_save"=>1,"a.user_material_uuid"=>$userMaterialUUID]);
        $query = $db->get();
        $data = $query->one();
        return $data;
    }

    public function getUserMaterialNums($userUUID)
    {
        $this->db->from($this->table);
        $this->db->where(["user_uuid"=>$userUUID]);
        $this->db->where(["status"=>2]);
        $this->db->where(["status1"=>1]);
        $this->db->where(["mix_status"=>1]);
        return $this->db->count_all_results(true);
    }

    /*=============================cron使用=====================*/
    public function getReadyHandleVideoMusic($limit)
    {
        $condition = array();
        $condition["eq"]["mix_status!="] = 1;
        $condition["eq"]["mix_count<"] = 3;
        $condition["page"]["p"] = 1;
        $condition["page"]["size"] = $limit;
        $condition["order"]["mix_count"] = "asc";
        return $this->getList($condition);
    }
}